package com.cs342.uwosh.cloudycontacts;
import java.util.ArrayList;

import com.cs342.uwosh.cloudycontacts.tasks.db.DeleteChosenContactsTask;
import com.cs342.uwosh.cloudycontacts.tasks.db.GetAllContactTask;
import com.cs342.uwosh.cloudycontacts.tasks.db.GetAllContactsChooserTask;
import com.cs342.uwosh.cloudycontacts.R.*;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class DeleteContactsChooser extends Activity {

	public static ListView listOfDeleteContacts;
	public static ArrayAdapter<Contact> deletecontactListAdapter;
	public static int sizeOfAdapter;
	public static Context mainContext;
	public static final int[] DEFAULT_THEME_ARRAY = 
			new int[] 
					{
					R.color.background_color_theme_default,
					R.color.category_btn_color_theme_default,
					R.color.list_item_color_theme_default,
					R.layout.custom_checkbox_theme_default,
					R.color.button_color_theme_default,
					R.style.ButtonText_theme_default,
					R.style.ButtonText_theme_default,
					R.style.ListItemText_theme_default,
					R.style.EditText_theme_default,
					R.color.category_btn_color_theme_default_pressed
					};
	
	public static final int[] ONE_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_one,
					R.color.category_btn_color_theme_one,
					R.color.list_item_color_theme_one,
					R.layout.custom_checkbox_theme1_default,
					R.color.button_color_theme_one,
					R.style.ButtonText_theme1_default,
					R.style.ButtonText_theme1_default,
					R.style.ListItemText_theme1_default,
					R.style.EditText_theme1_default,
					R.color.category_btn_color_theme_one_pressed
					};
	
	public static final int[] TWO_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_two,
					R.color.category_btn_color_theme_two,
					R.color.list_item_color_theme_two,
					R.layout.custom_checkbox_theme2_default,
					R.color.button_color_theme_two,
					R.style.ButtonText_theme2_default,
					R.style.ButtonText_theme2_default,
					R.style.ListItemText_theme2_default,
					R.style.EditText_theme2_default,
					R.color.category_btn_color_theme_two_pressed
					};
	
	public static final int[] THREE_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_three,
					R.color.category_btn_color_theme_three,
					R.color.list_item_color_theme_three,
					R.layout.custom_checkbox_theme3_default,
					R.color.button_color_theme_three,
					R.style.ButtonText_theme3_default,
					R.style.ButtonText_theme3_default,
					R.style.ListItemText_theme3_default,
					R.style.EditText_theme3_default,
					R.color.category_btn_color_theme_three_pressed
					};
	
	
	public static String THEME_DEFAULT = "DEFAULT";

	public static String THEME_ONE = "THEME_ONE";

	public static String THEME_TWO = "THEME_TWO";

	public static String THEME_THREE = "THEME_THREE";

	public static String CURRENT_THEME = THEME_DEFAULT;

	
	ArrayList<Contact> contactsToDelete = new ArrayList<Contact>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delete_contacts_chooser);
		
		mainContext = this;
		

		listOfDeleteContacts = (ListView) findViewById(R.id.deleteContactsListView);
		
		new GetAllContactsChooserTask().execute();
		
		listOfDeleteContacts.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
			{
				Contact clickedContact = deletecontactListAdapter.getItem(position);
				CheckBox addToCatChkBox = (CheckBox)view.findViewById(R.id.deleteContactCheckBox);
				addToCatChkBox.setChecked(!addToCatChkBox.isChecked());
				if(contactsToDelete.contains(clickedContact))
				{
					contactsToDelete.remove(clickedContact);
				}
				else
				{
					contactsToDelete.add(clickedContact);
				}
			}	
		});
		
		findViewById(R.id.cancel_delete_chooser).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				finishHim();
			}
		});
		findViewById(R.id.accept_delete_chooser).setOnClickListener(new OnClickListener()
		{
			@SuppressWarnings("unchecked")
			@Override
			public void onClick(View arg0) {
				new DeleteChosenContactsTask().execute(contactsToDelete);
				finishHim();
			}
		});
		
	}
	
	public void onResume()
	{
		super.onResume();
		if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_DEFAULT))
        {
			applyTheme(MainActivity.DEFAULT_THEME_ARRAY);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_ONE))
        {
        	applyTheme(MainActivity.ONE_THEME_ARRAY);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_TWO))
        {
        	applyTheme(MainActivity.TWO_THEME_ARRAY);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_THREE))
        {
        	applyTheme(MainActivity.THREE_THEME_ARRAY);
        }
		
		
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.themes:
			generateThemesDialog();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	private   void generateThemesDialog()
	{
		final CharSequence[] themesList = new CharSequence[] 
				{
				THEME_DEFAULT,
				THEME_ONE,
				THEME_TWO,
				THEME_THREE
				};
		AlertDialog.Builder themesDialog = new AlertDialog.Builder(this);
		
		themesDialog.setTitle("Choose Your Theme");
		
		themesDialog.setSingleChoiceItems(themesList, -1, new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				if (themesList[which].equals(THEME_DEFAULT))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_DEFAULT;
					applyTheme(DEFAULT_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_ONE))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_ONE;
					applyTheme(ONE_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_TWO))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_TWO;
					applyTheme(TWO_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_THREE))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_THREE;
					applyTheme(THREE_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				
			}
		});
		themesDialog.show();
	}
	public void applyTheme(int[] themeToApply)
	{
		findViewById(R.id.Delete_Choose).setBackgroundResource(themeToApply[0]);
		Button b = (Button) findViewById(R.id.cancel_delete_chooser);
		b.setBackgroundResource(themeToApply[4]);
		b.setTextAppearance(this, themeToApply[6]);
		b = (Button) findViewById(R.id.accept_delete_chooser);
		b.setBackgroundResource(themeToApply[4]);
		b.setTextAppearance(this, themeToApply[6]);
	}
	
	public void finishHim()
	{
		this.finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater()
				.inflate(R.menu.activity_delete_contacts_chooser, menu);
		return true;
	}

}